using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class TempPIDifferencesRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class TempPIDifferencesDbo : DatabaseObject
        {
            public TempPIDifferencesDbo()
            {
                Name = "TempPIDifferences";
                GeneratedClassName = "TempPIDifferences";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ArticleCode", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 20, Precision = 0, Scale = 0, GeneratedPropertyName = "ArticleCode" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreCode", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "StoreCode" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Date", ColumnType = "date", IsIdentity = false, IsNullable = false, Length = 3, Precision = 10, Scale = 0, GeneratedPropertyName = "Date" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "PIDiffUnits", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "PIDiffUnits" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "PIDiff", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "PIDiff" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ChargeOffsUnits", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "ChargeOffsUnits" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ChargeOffs", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "ChargeOffs" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Rebates", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Rebates" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ArticleId", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "ArticleId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreId", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "StoreId" });
              
            }
        }
		
		public static List<TempPIDifferenceRow> Select(Expression<Func<TempPIDifferenceRow, bool>> expression)
        {
            List<TempPIDifferenceRow> rows = new List<TempPIDifferenceRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<TempPIDifferenceRow>(new TempPIDifferencesDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<TempPIDifferenceRow> Select()
		{
			List<TempPIDifferenceRow> rows = new List<TempPIDifferenceRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<TempPIDifferenceRow>(new TempPIDifferencesDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<TempPIDifferenceRow> PagedSelect(Expression<Func<TempPIDifferenceRow, bool>> filterExpression, Expression<Func<TempPIDifferenceRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<TempPIDifferenceRow> rows = new List<TempPIDifferenceRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<TempPIDifferenceRow>(new TempPIDifferencesDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<TempPIDifferenceRow> TempPIDifferenceList)
        {
            TempPIDifferenceRow row;

            while (reader.Read())
            {
                row = new TempPIDifferenceRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					ArticleCode = Util.CastDbValue<string>(reader["ArticleCode"]),
					StoreCode = Util.CastDbValue<string>(reader["StoreCode"]),
					Date = Util.CastDbValue<DateTime>(reader["Date"]),
					PIDiffUnits = Util.CastDbValue<int?>(reader["PIDiffUnits"]),
					PIDiff = Util.CastDbValue<decimal?>(reader["PIDiff"]),
					ChargeOffsUnits = Util.CastDbValue<int?>(reader["ChargeOffsUnits"]),
					ChargeOffs = Util.CastDbValue<decimal?>(reader["ChargeOffs"]),
					Rebates = Util.CastDbValue<decimal?>(reader["Rebates"]),
					ArticleId = Util.CastDbValue<int?>(reader["ArticleId"]),
					StoreId = Util.CastDbValue<int?>(reader["StoreId"])
                };
                                
                TempPIDifferenceList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(TempPIDifferenceRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<TempPIDifferenceRow>(new TempPIDifferencesDbo(), row);
            }
        }

		public static void Update(TempPIDifferenceRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<TempPIDifferenceRow>(new TempPIDifferencesDbo(), row);
            }
        }

		public static void Delete(Expression<Func<TempPIDifferenceRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<TempPIDifferenceRow>(new TempPIDifferencesDbo(), expression);
            }
        }

	}
}
	
